package cn.getech.data.development.controller.real;

import cn.getech.data.development.constant.Constant;
import cn.getech.data.development.model.dto.RealTimeTaskMenuSaveDto;
import cn.getech.data.development.model.vo.RealTaskTypeMenuSearchVO;
import cn.getech.data.development.model.vo.RealTimeTaskMenuSearchVO;
import cn.getech.data.development.service.RealTimeTaskMenuService;
import cn.getech.data.intelligence.common.utils.R;
import cn.getech.system.center.annotation.SysLog;
import cn.getech.system.center.constant.OperationModulesEnum;
import cn.getech.system.center.constant.OperationTypeEnum;
import cn.getech.system.center.constant.module.ModuleEnum;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "实时任务菜单")
@RestController
@RequestMapping("/bdp/realTime/taskMenu")
public class RealTimeTaskMenuController {

    @Autowired
    private RealTimeTaskMenuService realTimeTaskMenuService;

    @ApiOperation("获取实时任务目录树")
    @ApiResponses({@ApiResponse(code = 200, message = "Success", response = RealTimeTaskMenuSaveDto.class)})
    @GetMapping("/list")
    public R menuList(RealTimeTaskMenuSearchVO vo) {
        if (vo.getProcId() == null) {
            return R.error("缺少项目id！");
        }
        return R.okWithData(realTimeTaskMenuService.menuList(vo));
    }

    @ApiOperation("保存菜单")
    @PostMapping("/save")
    @SysLog(value="保存实时任务菜单",type= OperationTypeEnum.UPDATECODE,
            modulesType = OperationModulesEnum.JOBMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    public R save(@RequestBody RealTimeTaskMenuSaveDto dto) {
        return realTimeTaskMenuService.saveOrUpdate(dto);
    }

    @ApiOperation("获取任务类型")
    @GetMapping("/taskTypes")
    public R taskTypes() {
        JSONArray array = new JSONArray();
        JSONObject object;
        Constant.RealTimeTaskType[] items = Constant.RealTimeTaskType.values();
        for (Constant.RealTimeTaskType item : items) {
            object = new JSONObject();
            object.put("code", item.getCode());
            object.put("name", item.getName());
            array.add(object);
        }
        return R.okWithData(array);
    }


    @ApiOperation("删除菜单")
    @DeleteMapping("/delete/{id}")
    @SysLog(value="删除实时任务菜单",type= OperationTypeEnum.DELETECODE,
            modulesType = OperationModulesEnum.JOBMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    public R delete(@PathVariable Integer id) {
        return realTimeTaskMenuService.deleteMenu(id);
    }


    @ApiOperation("根据项目id和类型获取实时任务")
    @GetMapping("/listByProcId")
    public R listByProcIdAndType(RealTaskTypeMenuSearchVO vo) {
        if (vo.getProcId() == null) {
            return R.error("缺少项目id！");
        }
        if (vo.getTaskType() == null) {
            return R.error("缺少任务类型！");
        }
        return R.okWithData(realTimeTaskMenuService.listByProcIdAndType(vo));
    }

}
